#!/bin/sh
# After install:
#  - Remove karaf user and group
#  - Change ownership of directories the karaf user needs to write to

set -e

create_daemon_user() {
    if ! getent passwd karaf >/dev/null ; then
        adduser --system --home /var/lib/karaf --group karaf
    fi 
}

change_karaf_files_ownership() {
    chown -R root:karaf /etc/karaf
    chmod -R 2750 /etc/karaf
    chmod 2770 /etc/karaf
    chown karaf:karaf /etc/karaf/*.cfg
    chmod g+w /etc/karaf/*.cfg
    chown karaf:karaf /etc/karaf/config.properties
    chmod g+w /etc/karaf/config.properties
    chown -R karaf:karaf /var/lib/karaf
    chown -R karaf:karaf /var/log/karaf
    chown -R root:root /usr/share/karaf
}

case "$1" in
    configure)
        mkdir -p /var/log/karaf
        create_daemon_user
        change_karaf_files_ownership
    ;;
esac

#DEBHELPER#

exit 0
